package org.cigo.fworld;

import org.cigo.fworld.R;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

public class Activity2 extends BaseActivity {
	 
	private String[] mStrings = {"中文成了乱码了", "乱马", "��Ŀ3"};
	private String str="";
	private EditText resultTextView;
	private int operator=0,
			flag=0;
	private double leftOp=0.0,
			rightOp=0.0,
			result=0.0;
	private View vi;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		Bundle bl = getIntent().getExtras();
		setTitle(String.valueOf(bl.getInt("title")));
		
		initTopBanner();
		initProjectListView();
		initSlidingDrawer();
		initCalculate();
	}

	private void initTopBanner() {
		Button leftBtn = getButtonLeft();
		if(null != leftBtn){
			
		}
		Button rightBtn = getButtonLeft();
		if(null != rightBtn){
			rightBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					
				}
			});
		}
	}

	private void initProjectListView() {
		// TODO Auto-generated method stub
		LinearLayout llProjectListView = (LinearLayout)findViewById(R.id.llProjectListView);
		ListView projListView = new ListView(this);
		projListView.setAdapter(new ArrayAdapter<String>(this,
	                android.R.layout.simple_list_item_1, mStrings));
		llProjectListView.addView(projListView);
	}

	private void initSlidingDrawer() {
		SlidingDrawer drawer = (SlidingDrawer)findViewById(R.id.slidingDrawer1);
		OnDrawerOpenListener onDrawerOpenListener = new OnDrawerOpenListener() {
			public void onDrawerOpened() {
				setTopBannerVisibility(false);
			}
		};
		OnDrawerCloseListener onDrawerCloseListener = new OnDrawerCloseListener() {
			public void onDrawerClosed() {
				setTopBannerVisibility(true);
			}
		};
		drawer.setOnDrawerOpenListener(onDrawerOpenListener);
		drawer.setOnDrawerCloseListener(onDrawerCloseListener);
	}

	private void initCalculate() {
		// TODO Auto-generated method stub
		//��ð���
		final Button numButton[]=new Button[10];
		final Button calcButtons[]=new Button[11];

		calcButtons[0]=(Button)findViewById(R.id.button01);
		calcButtons[1]=(Button)findViewById(R.id.button02);
		calcButtons[2]=(Button)findViewById(R.id.button03);
		calcButtons[3]=(Button)findViewById(R.id.button04);
		calcButtons[4]=(Button)findViewById(R.id.button05);
		calcButtons[5]=(Button)findViewById(R.id.button06);
		calcButtons[6]=(Button)findViewById(R.id.buttonce);
		calcButtons[7]=(Button)findViewById(R.id.buttonc);
		calcButtons[8]=(Button)findViewById(R.id.zheng);
		calcButtons[9]=(Button)findViewById(R.id.kaifang);
		calcButtons[10]=(Button)findViewById(R.id.pingfang);

		numButton[0]=(Button)findViewById(R.id.button0);
		numButton[1]=(Button)findViewById(R.id.button1);
		numButton[2]=(Button)findViewById(R.id.button2);
		numButton[3]=(Button)findViewById(R.id.button3);
		numButton[4]=(Button)findViewById(R.id.button4);
		numButton[5]=(Button)findViewById(R.id.button5);
		numButton[6]=(Button)findViewById(R.id.button6);
		numButton[7]=(Button)findViewById(R.id.button7);
		numButton[8]=(Button)findViewById(R.id.button8);
		numButton[9]=(Button)findViewById(R.id.button9);

		resultTextView=(EditText) findViewById(R.id.textView1);

		resultTextView.setText(str);
		calcButtons[6].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				str="";
				resultTextView.setText(str);
				vi=v;

			}
		});

		calcButtons[7].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				leftOp=0.0;operator=0;rightOp=0.0;
				str="";
				resultTextView.setText(str);

			}
		});

		calcButtons[8].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(vi!=calcButtons[5]&&str!=""){
					char ch=str.charAt(0);
					if(ch=='-')
						str=str.replace("-","");
					else
						str="-"+str;
					resultTextView.setText(str);
				}
			}
		});

		calcButtons[9].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str!=""){
					double a=Double.parseDouble(str);
					str=Math.sqrt(a)+"";
					resultTextView.setText(str);
				}}
		});

		calcButtons[10].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str!=""){
					double a=Double.parseDouble(str);
					str=""+a*a;
					resultTextView.setText(str);
				}}
		});
 
		numButton[0].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=0;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					char ch1[];
					ch1 = str.toCharArray();
					if(!(ch1.length==1&&ch1[0]=='0')) {
						str+=0;
						resultTextView.setText(str);
					}

				}
				vi=v;
			}
		});


		numButton[1].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=1;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=1;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[2].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=2;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=2;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[3].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=3;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=3;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[4].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=4;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=4;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[5].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=5;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=5;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[6].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=6;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=6;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[7].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=7;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=7;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[8].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=8;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=8;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		numButton[9].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==1){
					str="";
					str+=9;
					resultTextView.setText(str);
					flag=0;
				}
				else{
					str+=9;
					resultTextView.setText(str);
				}
				vi=v;
			}
		});

		//�趨��ż�        

		//��
		calcButtons[0].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str!=""){
					if(vi==calcButtons[0]||vi==calcButtons[1]||vi==calcButtons[2]||vi==calcButtons[3]){
						operator=1;
					}

					else{
						rightOp=Double.parseDouble(str);
						calculater();
						str=""+result;
						resultTextView.setText(str);
						operator=1;
						flag=1;
						vi=v;
					}}
			}
		});

		//��
		calcButtons[1].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str!=""){
					if(vi==calcButtons[0]||vi==calcButtons[1]||vi==calcButtons[2]||vi==calcButtons[3]){
						operator=2;
					}

					else{
						rightOp=Double.parseDouble(str);
						calculater();
						str=""+result;
						resultTextView.setText(str);
						operator=2;
						flag=1;
						vi=v;
					}}
			}
		});

		//��
		calcButtons[2].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str!=""){
					if(vi==calcButtons[0]||vi==calcButtons[1]||vi==calcButtons[2]||vi==calcButtons[3]){
						operator=3;
					}

					else{
						rightOp=Double.parseDouble(str);
						calculater();
						str=""+result;
						resultTextView.setText(str);
						operator=3;
						flag=1;
						vi=v;
					}}
			}
		});

		//��
		calcButtons[3].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str!=""){
					if(vi==calcButtons[0]||vi==calcButtons[1]||vi==calcButtons[2]||vi==calcButtons[3]){
						operator=4;
					}

					else{
						rightOp=Double.parseDouble(str);
						calculater();
						str=""+result;
						resultTextView.setText(str);
						operator=4;
						flag=1;
						vi=v;
					}}
			}
		});


		//�Ⱥ�        
		calcButtons[4].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str!=""&&vi!=calcButtons[0]&&vi!=calcButtons[1]&&vi!=calcButtons[2]&&vi!=calcButtons[3]){
					rightOp=Double.parseDouble(str);
					calculater();
					str=""+result;
					resultTextView.setText(str);
					flag=1;
					vi=v;
				}
			}
		});


		//С���
		calcButtons[5].setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(str==""){
					str+=".";
					resultTextView.setText(str);
				}
				else{
					char ch1[];int x=0;
					ch1=str.toCharArray();
					for(int i=0;i<ch1.length;i++)
						if(ch1[i]=='.')
							x++;
					if(x==0){
						str+=".";
						resultTextView.setText(str);
					}
				}

			}
		});
	}

	public double calculater(){
		switch(operator){
		case 0:result=rightOp;break;
		case 1:result=leftOp+rightOp;break;
		case 2:result=leftOp-rightOp;break;
		case 3:result=leftOp*rightOp;break;
		case 4:result=leftOp/rightOp;break;
		}

		leftOp=result;
		operator=0;

		return result;
	}
}
